<?php

namespace App\Http\Controllers\Home;


use Illuminate\Http\Request;
require_once app_path().'/Http/Org/code/Code.class.php';
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Org\code\Code;
use App\Models\Home\User;

class RegisterController extends Controller
{
    /*
     * 返回前台注册视图
     * @param
     * @return 注册视图
     * @author xxx
     * @date   2017/10/30  19:24
     */
    public function getIndex()
    {

        return view('Home.Register.register');
    }

    //返回验证码
    public function getCode()
    {
        $code = new Code;
        return $code->make();
    }

     /*
     * 处理前台手机注册操作
     * @param
     * @author xxx
     * @date   2017/10/30  20:40
     */
    public function postPhoneinsert(Request $request)
    {
        dd($request->all());
        // 验证验证码
        if(session('phonecode') != $request->input('phonecode')){
            echo '<script>alert("手机验证码错误")</script>';
        }else{
            echo '注册成功';
        }
    }

     /*
     * 专门发送手机短信验证
     * @param
     * @author xxx
     * @date   2017/10/30  20:40
     */
    public function getSendcode(Request $request)
    {
        // 使用互亿无线 
        $phone=$request->input('phone','');
        $user = User::where('phone', $phone)->first();
        if ($user) {
            return 1;
            // return redirect('/home/register/index')->withErrors($validator)->withInput();
        }
        $str=rand(100000,999999);
        session(['phonecode'=>$str]);
        //发送短信的接口
        $url='http://106.ihuyi.com/webservice/sms.php?method=Submit&account=C43563228&password=a2b99264680680128bb54d712c381e8e&mobile='.$phone.'&content=您的验证码是：'.$str.'。请不要把验证码泄露给其他人。';
        $ch = curl_init();
        // 添加apikey到header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 执行HTTP请求
        curl_setopt($ch , CURLOPT_URL , $url);
        $res = curl_exec($ch);
        $arr = json_decode ($res , true);
        // var_dump($res);
        echo $arr['code'];
    }

    


    
}
